//
//  LTMainController.swift
//  SwiftTest
//
//  Created by xxh on 2022/6/10.
//

import UIKit

class LTMainController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.delegate = self
        
        //添加子控制器
        addNavChildVCs();
        tabBar.tintColor = .red
    }
    
    func addNavChildVCs() {
        addChild(createChildVC(moduleVC: LTHomeVC.init(),name: "首页", tabbarImg: "home"))
        addChild(createChildVC(moduleVC: LTFundVC.init(),name: "基金", tabbarImg: "fund"))
        addChild(createChildVC(moduleVC: LTDiscoveryVC.init(),name: "发现", tabbarImg: "discovery"))
        addChild(createChildVC(moduleVC: LTAssetVC.init(),name: "资产", tabbarImg: "asset"))
    }
    
    
    func createChildVC(moduleVC: LTBaseViewController,name:String,tabbarImg:String) -> LTBaseNavigationViewController {
            
        let selectedImgName = tabbarImg + "_selected"
        moduleVC.tabBarItem.title = name
        moduleVC.tabBarItem.image = UIImage.init(named: tabbarImg)?.withRenderingMode(.alwaysOriginal)
        moduleVC.tabBarItem.selectedImage = UIImage.init(named: selectedImgName)?.withRenderingMode(.alwaysOriginal)
        let navVC = LTBaseNavigationViewController.init(rootViewController: moduleVC)
        return navVC
    }
    
}
extension LTMainController: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
       
        let navVC = viewController as? LTBaseNavigationViewController
        let className = navVC?.viewControllers.last as? LTBaseViewController
      
        //点击资产 && 未登录 -->调起登录方法
        if (className is LTAssetVC && LTGlobal.sharedInstance.isLogin == false) {
            className?.login()
        }
        
        return true
    }
}
